home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Disc to the Future 2
/
Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin
/
MAC
/
THINKC
/
4_0
/
LIFE6__
/
PROTO
/
P
/
PW_LIFE_.C
< prev
next >
Wrap
Text File
|
1991-08-16
|
15KB
|
508 lines
/* PW_Life_4a
File name: Life_4a
Function: Handle a Window
/* History: 8/13/91 Original by Prototyper 3.0 */
#include "PCommonLife6.h" /* Common */
#include "Common_Life6.h" /* Common */
#include "PUtils_Life6.h" /* General Utilities */
#include "Utils_Life6.h" /* General Utilities */
#include "PW_Life_4a.h" /* This file */
#include "Life_4a.h" /* The window specific file */
static struct {
long Id;
STATUS_TYPE Health;
ControlHandle CHandle;
Rect CRect;
}map[MaxRow][MaxCol], new_map[MaxRow][MaxCol];
/* ======================================================= */
static ControlHandle ScrollHHandle; /* Scrollbar for horz scrolling */
static ControlHandle ScrollVHandle; /* Scrollbar for vert scrolling */
static void Do_A_Button(ControlHandle theControl); /* Handle a button being pressed */
/* ======================================================= */
/* Routine: Init_Life_4a */
/* Purpose: Initialize our window data to not in use yet */
void Init_Life_4a()
{
WPtr_Life_4a = NIL; /* Make sure other routines know we are not valid yet */
ScrollHHandle = NIL; /* Scrollbar is not valid yet */
ScrollVHandle = NIL; /* Scrollbar is not valid yet */
U_Init_Life_4a(); /* Call the user window init routine */
}
/* ======================================================= */
/* Routine: Close_Life_4a */
/* Purpose: Close out the window */
void Close_Life_4a(whichWindow)
WindowPtr whichWindow;
{
if ((WPtr_Life_4a != NIL) && ((WPtr_Life_4a == whichWindow) || (whichWindow == (WindowPtr)-1))) /* See if we should close this window */
{
U_Close_Life_4a(); /* Call the user window close routine */
DisposeWindow(WPtr_Life_4a); /* Clear window and controls */
WPtr_Life_4a = NIL; /* Make sure other routines know we are closed */
} /* End for if (MyWindow<>nil) */
} /* End of procedure */
/* ======================================================= */
/* Routine: Resized_Life_4a */
/* Purpose: We were resized or zoomed, update the scrolling scrollbars */
void Resized_Life_4a(OldRect, whichWindow) /* Resized this window */
Rect *OldRect;
WindowPtr whichWindow;
{
WindowPtr SavePort; /* Place to save the last port */
Rect temp2Rect; /* temp rectangle */
short Index; /* temp integer */
if (WPtr_Life_4a == whichWindow) /* Only do if the window is us */
{
GetPort(&SavePort); /* Save the current port */
SetPort(WPtr_Life_4a); /* Set the port to my window */
U_Resized_Life_4a(OldRect); /* Call the user window resized routine */
temp2Rect = WPtr_Life_4a->portRect; /* Get the window rectangle */
EraseRect(&temp2Rect); /* Erase the new window area */
InvalRect(&temp2Rect); /* Set to update the new window area */
SetPort(SavePort); /* Restore the old port */
} /* End for window is us */
} /* End of procedure */
/* ======================================================= */
/* Routine: Moved_Life_4a */
/* Purpose: We were moved, possibly to another screen and screen depth */
void Moved_Life_4a(OldRect, whichWindow) /* Moved this window */
Rect *OldRect;
WindowPtr whichWindow;
{
WindowPtr SavePort; /* Place to save the last port */
if (WPtr_Life_4a == whichWindow) /* Only do if the window is us */
{
GetPort(&SavePort); /* Save the current port */
SetPort(WPtr_Life_4a); /* Set the port to my window */
U_Moved_Life_4a(OldRect); /* Call the user window moved routine */
SetPort(SavePort); /* Restore the old port */
} /* End for window is us */
} /* End of procedure */
/* ======================================================= */
/* Routine: Update_Life_4a */
/* Purpose: Update our window */
void Update_Life_4a(whichWindow)
WindowPtr whichWindow;
{
WindowPtr SavePort; /* Place to save the last port */
if ((WPtr_Life_4a != NIL) && (WPtr_Life_4a == whichWindow))/* Handle the update to our window */
{
GetPort(&SavePort); /* Save the current port */
SetPort(WPtr_Life_4a ); /* Set the port to my window */
TextFont(systemFont); /* Select the Font that we want */
/* Draw a string of text, Static Text */
SetRect(&tempRect, 16,0,236,15);
GetIndString(sTemp,Res_Ctrl_Static_Text,1); /* Get the string */
TextBox(&sTemp[1], sTemp[0], &tempRect, teJustLeft);
/* Draw a string of text, Static Text */
SetRect(&tempRect, 16,16,232,31);
GetIndString(sTemp,Res_Ctrl_Static_Text2,1); /* Get the string */
TextBox(&sTemp[1], sTemp[0], &tempRect, teJustLeft);
/* Draw a string of text, Static Text */
SetRect(&tempRect, 84,32,164,47);
GetIndString(sTemp,Res_Ctrl_Static_Text3,1); /* Get the string */
TextBox(&sTemp[1], sTemp[0], &tempRect, teJustLeft);
/* Draw a line, Drawn line1 */
PenSize(3,3); /* Change pen size to draw a thicker line */
MoveTo(0,160); /* Horz,vert, Move to starting position */
LineTo ( 255,160); /* Horz,vert, Draw to ending position */
PenSize(1,1); /* Restore the pen size to default */
TextFont(applFont); /* Back to the application font */
U_Update_Life_4a(); /* Call the user window update routine */
DrawControls(WPtr_Life_4a ); /* Draw all the controls */
SetPort(SavePort); /* Restore the old port */
} /* End for if (MyWindow<>nil) */
} /* End of procedure */
/* ======================================================= */
/* Routine: Open_Life_4a */
/* Purpose: Open our window */
void Open_Life_4a()
{
register counterX = 1;
register counterY = 1;
register index = LoBase;
long theLong; /* Used for icons and hotspots definition */
if (WPtr_Life_4a == NIL) /* See if already opened */
{
WPtr_Life_4a= GetNewWindow(Res_W_Life_4a,NIL, (Ptr)-1);/* Get the window from the resource file */
tempRect = WPtr_Life_4a->portRect; /* Get window size, we will now center it */
tempRect.top = ((screenBits.bounds.bottom - screenBits.bounds.top) - (tempRect.bottom - tempRect.top)) / 2;/* Center vert */
if (tempRect.top < 40) /* Keep below the menu bar */
tempRect.top = 40;
tempRect.left = ((screenBits.bounds.right - screenBits.bounds.left) - (tempRect.right - tempRect.left)) / 2;/* Center Horz */
MoveWindow(WPtr_Life_4a, tempRect.left, tempRect.top, TRUE);/* Now move the window to the proper position */
SetPort(WPtr_Life_4a); /* Prepare to write into our window */
/* Make a button, GO */
Ctrl_GO = GetNewControl( Res_Ctrl_GO, WPtr_Life_4a);/* Make a new button */
/* INITIALIZE THE ZONES & ARRAY HERE */
for (; index <= HiBase; index++)
{
/* xxx */
map[counterY][counterX].CHandle = GetNewControl( index, WPtr_Life_4a);/* Make a new HotSpot */
HideControl(map[counterY][counterX].CHandle); /* Hide till we set all the parameters */
theLong = 0x00FF0000; /* Tell the CDEF that it is a HotSpot */
theLong = theLong + (long)((0x00FF & 2) * 0x00000100);/* Set the line width parameter */
(*map[counterY][counterX].CHandle)->contrlData = (Handle)theLong;/* Set all the parameters */
map[counterY][counterX].CRect = (*map[counterY][counterX].CHandle)->contrlRect;/* Get the position */
ShowControl(map[counterY][counterX].CHandle); /* Show since we set all the parameters */
map[counterY][counterX].Id = index;
if (counterX < (MaxCol - 1))
{
++counterX;
}
else
{
++counterY;
counterX = 1;
}
}
U_Open_Life_4a(); /* Call the users window open routine */
ShowWindow(WPtr_Life_4a); /* Show the window now */
} /* End for if (MyWindow<>nil) */
else
SelectWindow(WPtr_Life_4a); /* Already open, so show it */
} /* End of procedure */
/* ======================================================= */
/* Routine: Activate_Life_4a */
/* Purpose: We activated or deactivated. */
void Activate_Life_4a(whichWindow,Do_An_Activate) /* Activated or deactivated this window */
WindowPtr whichWindow;
Boolean Do_An_Activate;
{
WindowPtr SavePort; /* Place to save the last port */
if (WPtr_Life_4a == whichWindow) /* Only do if the window is us */
{
GetPort(&SavePort); /* Save the current port */
SetPort(WPtr_Life_4a); /* Set the port to my window */
if (Do_An_Activate == TRUE) /* Handle the activate */
{
} /* End for activate */
else
{ /* Start of deactivate */
if (theInput != NIL) /* See if there is already a TE area */
TEDeactivate(theInput); /* Yes, so turn it off */
theInput = NIL; /* Deactivate the TE area */
} /* End for deactivate */
U_Activate_Life_4a(Do_An_Activate); /* Call the user window activate routine */
SetPort(SavePort); /* Restore the old port */
} /* End for window is us */
} /* End of procedure */
/* ======================================================= */
/* Handle a button pressed */
static void Do_A_Button(theControl) /* Handle a button being pressed */
ControlHandle theControl;
{
long RefCon; /* RefCon for controls */
register counterX = 1;
register counterY = 1;
HiliteControl(theControl, 10); /* Darken the button */
RefCon = GetCRefCon(theControl); /* get control refcon */
if (RefCon == Res_Ctrl_GO)
{
/* THROW PROCEDURE CALL TO COMPUTE GENERATION HERE */
COMPUTE();
}
/***************************************************************/
/* THIS ROUTINE CHECKS FOR THE STATUS OF A CELL WHEN CLICKED */
while (map[counterY][counterX].Id != RefCon)
{
if (counterX < (MaxCol - 1))
{
++counterX;
}
else
{
++counterY;
counterX = 1;
}
}
if ((map[counterY][counterX].Id == RefCon) && (map[counterY][counterX].Health == DEAD))
{
map[counterY][counterX].Health = ALIVE;
InvertRect (&map[counterY][counterX].CRect);
}
else if ((map[counterY][counterX].Id == RefCon) && (map[counterY][counterX].Health == ALIVE))
{
map[counterY][counterX].Health = DEAD;
InvertRect (&map[counterY][counterX].CRect);
}
/***************************************************************/
/* end of switch */
U_DoButton_Life_4a(RefCon,theControl); /* Call to handle any extra user buttons */
HiliteControl(theControl, 0); /* Lighten the button */
} /* End of Handle a button being pressed */
/* ======================================================= */
/* Routine: Do_Life_4a */
/* Purpose: Handle action to our window, like controls */
void Do_Life_4a(myEvent)
EventRecord *myEvent;
{
short code; /* Location of event in window or controls */
WindowPtr whichWindow; /* Window pointer where event happened */
Point myPt; /* Point where event happened */
ControlHandle theControl; /* Handle for a control */
if (WPtr_Life_4a != NIL) /* Handle only when the window is valid */
{
code = FindWindow(myEvent->where, &whichWindow);/* Get where in window and which window */
if (WPtr_Life_4a == whichWindow)
U_DoEvent_Life_4a(myEvent);
if ((myEvent->what == mouseDown) && (WPtr_Life_4a == whichWindow))
{
myPt = myEvent->where; /* Get mouse position */
GlobalToLocal(&myPt); /* Make it relative */
}
if ((WPtr_Life_4a == whichWindow) && (code == inContent))/* for our window */
{
code = FindControl(myPt, whichWindow, &theControl);/* Get type of control */
if (code != 0) /* Check type of control */
code = TrackControl(theControl,myPt, (Ptr)-1);/* Track the control */
if (code == inButton)
Do_A_Button(theControl); /* Do buttons */
} /* End for if (MyWindow=whichWindow) */
} /* End for if (MyWindow<>nil) */
} /* End of procedure */
/* ======================================================= */
/* ======================================================= */
COMPUTE(void)
{
int counterY,counterX;
for(counterY=1;counterY <= (MaxRow - 1);counterY++)
{
for(counterX=1;counterX <= (MaxCol - 1);counterX++)
{
switch(NEIGHBORCOUNT(counterY,counterX))
{/* zzz */
case 0:
case 1:
new_map[counterY][counterX].Health = DEAD;
break;
case 2:
new_map[counterY][counterX].Health = map[counterY][counterX].Health;
break;
case 3:
new_map[counterY][counterX].Health = ALIVE;
break;
case 4:
case 5:
case 6:
case 7:
case 8:
new_map[counterY][counterX].Health = DEAD;
break;
}/* switch */
}/* for counterX */
}/* for counterY*/
CopyMap();
}
/* ======================================================= */
int NEIGHBORCOUNT(int countY, int countX)
{
int i,j;
int rlow,rhigh;
int clow, chigh;
int count = 0;
if (countY <= 0)
rlow = 0;
else
rlow = countY - 1;
if (countY >= MaxRow - 1)
rhigh = MaxRow - 1;
else
rhigh= countY + 1;
if (countX <= 0)
clow = 0;
else
clow = countX - 1;
if (countX >= MaxCol - 1)
chigh = MaxCol - 1;
else
chigh= countX + 1;
for (i = rlow; i <= rhigh; i++)
for (j = clow; j <= chigh; j++)
if (map[i][j].Health == ALIVE)
count++;
if (map[countY][countX].Health == ALIVE)
count--;
return count;
}
/* ======================================================= */
CopyMap()
{
int counterY,counterX;
for(counterY=1;counterY <= (MaxRow - 1);counterY++)
{
for(counterX=1;counterX <= (MaxCol - 1);counterX++)
{
if (map[counterY][counterX].Health != new_map[counterY][counterX].Health)
{
InvertRect (&map[counterY][counterX].CRect);
}
map[counterY][counterX].Health = new_map[counterY][counterX].Health;
}
}
DrawControls(WPtr_Life_4a );
}
/* ======================================================= */
RESET()
{
int counterY,counterX;
for(counterY=1;counterY <= (MaxRow - 1);counterY++)
for(counterX=1;counterX <= (MaxCol - 1);counterX++)
{
if (map[counterY][counterX].Health == ALIVE)
{
map[counterY][counterX].Health = DEAD;
InvertRect (&map[counterY][counterX].CRect);
}
}
DrawControls(WPtr_Life_4a );
}
/* ======================================================= */